home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 11 / AMUG BBS in a Box Volume XI (April 1994) (MacWizards).iso / Files / Tele / C / Comet2.1.3.sit / Comet / tftp_common.c / tftp_common.c
Encoding:
Text File  |  1990-09-26  |  2.8 KB  |  148 lines  |  [TEXT/MPS ]

  1. int (*tfs_alert)();
  2. int (*tfs_done)();
  3.  
  4. char macfile[256];            /* for Macintosh file name translation */
  5.  
  6. int ntftps = 0;
  7.  
  8.  
  9. int tfstate = OFF;
  10. long refusedt = 0;            /*  time of most recent transfer refusal  */
  11.  
  12. /* Setup a TFTP connection block. */
  13.  
  14. tfmkcn(cn, dir, mode)
  15.     register struct tfconn * cn;
  16.     unsigned dir;
  17.     unsigned mode; 
  18. {
  19.     cn->tf_fd = NULL;
  20.     cn->tf_udp = NULL;
  21.     cn->tf_rcv = NULL;
  22.     cn->tf_snt = 0;
  23.     cn->tf_ous = 0;
  24.     cn->tf_ntmo = 0;
  25.     cn->tf_rsnd = 0;
  26.     cn->tf_dir = dir;
  27.     cn->tf_mode = mode;
  28.     cn->tf_size = 0L;
  29.     cn->tf_K = Kinit;
  30.     cn->tf_trt = T0;
  31.     cn->tf_rt = (long) min(cn->tf_trt * TMMULT, MAXTMO);
  32.     cn->tf_NR = 0;
  33.     cn->tf_NR_last = 1;
  34.  
  35.     cn->tf_tm = tm_alloc();
  36.     if (cn->tf_tm == NULL) {
  37.         error("TFTP: Couldn't allocate timer");
  38.         return(-1);
  39.     }
  40.     cn->tf_outp = malloc(NORMLEN);
  41.     if (cn->tf_outp == NULL) {
  42.         error("TFTP: Couldn't allocate output packet");
  43.         tm_free(cn->tf_tm);
  44.         return(-2);
  45.     }
  46.     return(0);
  47. }
  48.  
  49.  
  50. /* Send a TFTP data block */
  51.  
  52. tfsndata(cn, len)
  53.     register struct tfconn *cn;
  54.     unsigned len; 
  55. {
  56.     register struct tfdata *tfdata;
  57.  
  58.     tfdata = cn->tf_outp;
  59.     tfdata->tf_op = DATA;
  60.     tfdata->tf_block = cn->tf_expected;
  61.  
  62. #ifdef TFTPDEBUG
  63.     if (NDEBUG & APTRACE) 
  64.         printf("TFTP:  sending block %u\n", tfdata->tf_block);
  65. #endif
  66.     return(tf_write(cn, sizeof(struct tfdata) - 512 + len));
  67. }
  68.  
  69. /* Process an incoming error packet */
  70.  
  71. tfdoerr(cn, perr, len)
  72.     register struct tfconn *cn;
  73.     register struct tferr *perr;
  74.     unsigned len; 
  75. {
  76.     char terror[100];
  77.  
  78.     sprintf(terror, "TFTP: Error from host: \"%s\"", perr->tf_err);
  79.     error(terror);
  80. }
  81.  
  82.  
  83. /* Format up and send out an initial request for a tftp connection. */
  84.  
  85. tfsndreq(cn, fname)
  86.     register struct tfconn *cn;
  87.     char *fname; 
  88. {
  89.     register struct tfreq *ptreq;
  90.     unsigned reqlen;
  91.  
  92.     ptreq = (struct tfreq *) cn->tf_outp;
  93.     if (cn->tf_dir == GET) 
  94.         ptreq->tf_op = RRQ;
  95.     else if (cn->tf_dir == PUT) 
  96.         ptreq->tf_op = WRQ;
  97.     else {
  98. #ifdef TFTPDEBUG
  99.         printf("TFSNDREQ: Bad direction %u.\n", cn->tf_dir);
  100.         tfcndump(cn);
  101.         if (NDEBUG & BUGHALT) 
  102.             cu_exit(1);
  103. #endif
  104.         return(-1);
  105.     }
  106.  
  107.     strcpy(&ptreq->tf_name[0], fname);
  108.     if (cn->tf_mode == IMAGE || cn->tf_mode == TEST)
  109.         strcpy(&ptreq->tf_name[0] + strlen(fname) + 1, "image");
  110.     else if (cn->tf_mode == OCTET)
  111.         strcpy(&ptreq->tf_name[0] + strlen(fname) + 1, "octet");
  112.     else if (cn->tf_mode == ASCII)
  113.         strcpy(&ptreq->tf_name[0] + strlen(fname) + 1, "netascii");
  114.     else {
  115. #ifdef TFTPDEBUG
  116.          printf("TFSNDREQ: Bad mode %u.\n", cn->tf_mode);
  117.         tfcndump(cn);
  118.         if (NDEBUG & BUGHALT) 
  119.             cu_exit(1);
  120. #endif
  121.         return(-1);
  122.     }
  123.  
  124. #ifdef TFTPDEBUG
  125.     if (NDEBUG & APTRACE) 
  126.         printf("TFTP:  sending initial request\n");
  127. #endif
  128.     return(tf_write(cn, sizeof(struct tfreq) + strlen(fname)));
  129. }
  130.  
  131.  
  132.  
  133.  
  134. static unsigned socket = 0;
  135.  
  136. unsigned short udp_socket() 
  137. {
  138.     if (socket) 
  139.         return socket++;
  140.  
  141.     socket = cticks;
  142.     if (socket < 1000) 
  143.         socket +=1000;
  144.     return(socket++);
  145. }
  146.  
  147.  
  148.